package com.example.lenovo.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;


public class MainActivity extends Activity {
    public TextView tv_accept;
    public EditText et_Send;
    public Button bnt_Transmit;
    public Socket mSocket;
    private Handler mHandler = new Handler(){
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int arg1 = msg.arg1;
            String name = (String)msg.obj;
            tv_accept.setText(name);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bnt_Transmit = (Button)findViewById(R.id.bnt_Transmit);
        et_Send = (EditText)findViewById(R.id.et_Send);
        tv_accept = (TextView)findViewById(R.id.tv_Accept);

        bnt_Transmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    OutputStream outputStream = mSocket.getOutputStream();
                    byte[] buffer = et_Send.getText().toString().getBytes();
                    outputStream.write(buffer);

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });
        new Thread(mSocketRunnable).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private Runnable mSocketRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                mSocket = new Socket("120.55.43.147", 9999);
                InputStream inputStream = mSocket.getInputStream();
                byte[] buffer = new byte[1024];
                while (true) {

                    int readLength = inputStream.read(buffer);
                    Message msg = new Message();
                    String contant = new String(buffer, 0, readLength);
                    msg.obj = contant;
                    mHandler.sendMessage(msg);
                    if (contant.equals("bye")){
                        break;
                    }
                }
                mSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    };
}
